package com.miplus.ccrm.core.service.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionRatioEditVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionRatioListReqVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionRatioReqVo;
import com.miplus.ccrm.core.controller.vo.commission.CommissionRatioResVo;
import com.miplus.ccrm.core.pojo.FiCommission;
import com.miplus.ccrm.core.service.commission.FiCommissionService;
import com.miplus.ccrm.core.mapper.FiCommissionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author wangzl
* @description 针对表【fi_commission(佣金比例管理表)】的数据库操作Service实现
* @createDate 2025-03-01 17:52:19
*/
@Service
public class FiCommissionServiceImpl extends MPJBaseServiceImpl<FiCommissionMapper, FiCommission>
    implements FiCommissionService{
    @Override
    public IPage<CommissionRatioResVo> getCommissionRatioList(CommissionRatioListReqVo para)
    {
        return selectJoinListPage(
                new Page<>(para.getPageNum(),para.getPageSize()),
                CommissionRatioResVo.class,
                new MPJLambdaWrapper<FiCommission>()
                        .leftJoin("sys_grp g on g.grpid=t.dept")
                        .eq(StringUtils.isNotEmpty(para.getDisttag()),FiCommission::getDisttag,para.getDisttag())
                        .eq(StringUtils.isNotEmpty(para.getDept()),FiCommission::getDept,para.getDept())
                        .ge(StringUtils.isNotEmpty(para.getStartDate()),FiCommission::getStartdate,para.getStartDate())
                        .le(StringUtils.isNotEmpty(para.getEndDate()),FiCommission::getStartdate,para.getEndDate())
                        .selectAll(FiCommission.class)
                        .selectAs("fun_getnames('SORTING.QUEUE',t.disttag)",CommissionRatioResVo::getDisttagDesc)
                        .selectAs("g.grpname",CommissionRatioResVo::getDeptName)
        );
    }
    @Override
    public CmdResVo addCommissionRatio(String usrId, FiCommission fiCommission)
    {
        fiCommission.setId(null);
        fiCommission.setCrdt(new Date());
        fiCommission.setCrusr(usrId);
        fiCommission.setOverdue(String.format("M%s-%s",fiCommission.getOverdueday1(),fiCommission.getOverdueday2()));
        save(fiCommission);
        return new CmdResVo(1, fiCommission.getId().toString());
    }
    @Override
    public CmdResVo updateCommissionRatio(String usrId, CommissionRatioEditVo para)
    {
        if( ObjectUtils.isNull(para.getId()))
            return new CmdResVo(-1, "没有指定更新的ID");
        FiCommission fiCommission = new FiCommission();
        BeanUtils.copyProperties(para,fiCommission);
        fiCommission.setOverdue(String.format("M%s-%s",fiCommission.getOverdueday1(),fiCommission.getOverdueday2()));
        fiCommission.setMdusr(usrId);
        fiCommission.setMddt(new Date());
        updateById(fiCommission);
        return new CmdResVo(1, "更新成功");
    }
    @Override
    public CmdResVo deleteCommissionRatio(Integer id)
    {
        removeById(id);
        return new CmdResVo(1, "删除成功");
    }
    @Override
    public FiCommission getCommisionRatioByMon(CommissionRatioReqVo para)
    {
        List<FiCommission> fiCommissions =  baseMapper.selectList( new QueryWrapper<FiCommission>()
                .eq("disttag",para.getDisttag())
                .eq("dept",para.getDept())
                .le("startdate",para.getStartDate())
                .le("overdueday1",para.getOverdueDay())
                .ge("overdueday2",para.getOverdueDay())
                .orderByDesc("startdate")
        );
        if(fiCommissions != null && fiCommissions.size()>0)
            return fiCommissions.get(0);
        else
            return null;
    }

}




